//
//  SignTabCell.swift
//  nurseApp
//
//  Created by cyptt on 2022/7/8.
//

import UIKit
open class CHSelectAllTabCell: UITableViewCell {
    
    
    public var isEdit:Bool = false  //是否编辑模式 默认false 不编辑
    public var isSelect:Bool = false //是否选择 默认false 不选择
    
    public var SELECT_LEFT_W:CGFloat = 0
    
    public var signSelectModel:CHSelectAllModel?
    
    public var selectBtnCallback:((_ btn:UIButton,_ selectAllModel:CHSelectAllModel) -> ())?
    open  override func awakeFromNib() {
        super.awakeFromNib()
        
        
        self.setUI()
        
    }
    
    public  override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    
    private  func setUI(){
        
  
        weak var weakSelf = self
        self.addSubview(self.leftView)
        
    
        self.selectSingleBtn.btnClickCallBack { btn in
            btn.isSelected = !btn.isSelected
            //改变数据
            weakSelf?.signSelectModel?.isSelect = btn.isSelected
            
            if weakSelf?.selectBtnCallback != nil{
                weakSelf?.selectBtnCallback!(btn, weakSelf!.signSelectModel!)
            }
            
            
        }
        
        
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    
    public func btnClickCallBack(closure:@escaping(_ btn:UIButton,_ selectAllModel:CHSelectAllModel)-> Void){
        
        self.selectBtnCallback = closure
    }
    open  override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    
    
    public  func setSelectAllModel(signEidtIem:CHSelectAllModel){
        self.signSelectModel = signEidtIem
        if signEidtIem.isEdit == false{
            
            self.selectionStyle = UITableViewCell.SelectionStyle.none
            
            self.SELECT_LEFT_W = 0
        }else{
            
            self.SELECT_LEFT_W = 50
        }
        self.selectSingleBtn.isSelected = signEidtIem.isSelect ?? false
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        let SelfW:CGFloat = self.frame.size.width
        let SelfH:CGFloat = self.frame.size.height
        self.contentView.frame = CGRect.init(x: self.SELECT_LEFT_W, y: 0, width:SelfW , height: SelfH)
        
        self.leftView.frame =  CGRect.init(x: 0, y: 0, width:self.SELECT_LEFT_W , height: SelfH)
        if self.SELECT_LEFT_W > 0 {
            
            self.selectSingleBtn.isHidden = false
        }else{
            
            self.selectSingleBtn.isHidden = true
        }
        
        self.selectSingleBtn.center = self.leftView.center
    }
    
    public lazy var selectSingleBtn = {()->CHClickEdgeInsetsButton in
        
        let img:UIImage? =  UIImage.init(named: "check_box_normal", in: CHBundle.bundle(), compatibleWith: nil)
        let imged:UIImage? =  UIImage.init(named: "check_box_selected", in: CHBundle.bundle(), compatibleWith: nil)
        let btn = CHClickEdgeInsetsButton.init(type: .custom)
        btn.setImage(img, for: .normal)
        btn.setImage(imged, for: .selected)
        btn.frame = CGRect.init(x: 0, y: 0, width: 18, height: 18)
        btn.isHidden = true
        
        
        return btn
    }()
    
    public  lazy var leftView = {()->UIView in
        
        let v:UIView = UIView.init()
        v.backgroundColor = UIColor.clear
        v.addSubview(self.selectSingleBtn)
        return v
    }()
}

public extension CHSelectAllTabCell{
    /*
     获取已经选择的下标数组
     */
    static func getSelectedIndexArr(slectAllArr:[CHSelectAllModel])->[Int]{
        var indexArr:[Int] = []
        for selectAllModel in slectAllArr {
            if selectAllModel.isSelect == true{
                indexArr.append(selectAllModel.id!)
            }
        }
        return indexArr
    }
    
    /*
     是否包含有选择的
     */
    static func isHaveSelect(slectAllArr:[CHSelectAllModel])->Bool{
        var isHaveSelect:Bool = true
        
        if slectAllArr.count > 0{
            for i in 0..<slectAllArr.count{
                
                
                let model = slectAllArr[i]
                
                if model.isSelect == true{
                    
                    
                    isHaveSelect = true
                    break
                    
                }else{
                    isHaveSelect = false
                }
            }
        }else{
            isHaveSelect = false
        }
       
        return isHaveSelect
    }
    /*
     是否包含没有选择的
     */
    static func isAllSelect(slectAllArr:[CHSelectAllModel])->Bool{
        var isHaveNoSelect:Bool = false
        for i in 0..<slectAllArr.count{
            
            
            let model = slectAllArr[i]
            
            if model.isSelect == false{
                
                
                isHaveNoSelect = false
                break
                
            }else{
                isHaveNoSelect = true
            }
        }
        return isHaveNoSelect
    }
    /*
     显示/隐藏 数据
     */
    static func showOrhideAllArr(isShow:Bool,slectAllArr:[CHSelectAllModel])->[CHSelectAllModel]{
        var newArr:[CHSelectAllModel] = []
        for i in 0..<slectAllArr.count{
            print(i)
            let model = slectAllArr[i]
            model.isEdit = isShow
            newArr.append(model)
        }
        return newArr
    }
    
    /*
     全选/全不选
     */
    static func selectedOrCancelAllArr(isSelect:Bool,slectAllArr:[CHSelectAllModel])->[CHSelectAllModel]{
        var newArr:[CHSelectAllModel] = []
        for i in 0..<slectAllArr.count{
            print(i)
            let model = slectAllArr[i]
            //            model.isEdit = isSelect
            model.isSelect = isSelect
            newArr.append(model)
        }
        return newArr
    }
    
    /*
     单个按钮点击
     */
    static func changeSigle(slectAllArr:[CHSelectAllModel],selectModel:CHSelectAllModel?)->[Any]{
        //改变数据
        
        var isHaveNoSelect:Bool = false
        var newArr:[CHSelectAllModel] = []
        
        for i in 0..<slectAllArr.count{
            
            
            let model = slectAllArr[i]
            if model.id == selectModel?.id{
                model.isSelect = selectModel?.isSelect
            }
            
            
            newArr.append(model)
            
            if model.isSelect == false{
                
                
                isHaveNoSelect = true
                
            }
        }
        
        return [!isHaveNoSelect,newArr]
        
    }
    
}
